package com.ml.workmq.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ml
 * @date 2023/4/4 9:43
 * 同步消息测试
 */
@Component
public class TestProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer mqProducer = new DefaultMQProducer("test");
        mqProducer.setNamesrvAddr("101.43.234.92:9876");
        mqProducer.start();
        mqProducer.setAsyncSenderExecutor(new ThreadPoolExecutor(2,2,0, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>()));
//        List<Message> messages = new ArrayList<>();
//        Message message = null;
//        messages.add(new Message("test-topic1", ("hello world:01").getBytes(StandardCharsets.UTF_8)));
//        messages.add(new Message("test-topic1", ("hello world:02").getBytes(StandardCharsets.UTF_8)));
//        messages.add(new Message("test-topic1", ("hello world:03").getBytes(StandardCharsets.UTF_8)));
        Message message = new Message("test-topic1", ("hello world:01").getBytes(StandardCharsets.UTF_8));
        //延时消息 等级4 对应 1min
//        message.setDelayTimeLevel(4);
        mqProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
        // 如果不再发送消息，关闭Producer实例。
        mqProducer.shutdown();
    }
}
